package com.library.service.impl;

import com.library.bean.User;
import com.library.dao.UserDao;
import com.library.dao.impl.UserDaoImpl;
import com.library.service.UserService;

import java.util.HashMap;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private final UserDao userDao = new UserDaoImpl();

    @Override
    public Map<String, Object> user$login(String user$account, String user$pass) {
        Map<String, Object> map = null;
        User user = userDao.queryByUser(user$account, user$pass);
        if (user != null) {
            map = new HashMap<String, Object>();
            map.put("success", user);
        }else{
            map = new HashMap<String, Object>();
            user = userDao.queryByUser_account(user$account);
            if (user != null) {
                map.put("fail", "密码错误");
            }else{
                map.put("fail", "账号不存在");
            }
        }
        return map;
    }

    @Override
    public int user$register(User user) {
        if (user == null) {
            System.out.println("用户信息为空!");
            return -1;
        }
        User u = new User();
        u = userDao.queryByUser_account(user.getUser_account());
        if (u != null) {
            System.out.println("账号名已被注册!");
            return -1;
        }
        int row_num = userDao.insertUser(user);
        if (row_num != 1) {
            System.out.println("注册失败!");
            return -1;
        }
        return row_num;
    }
}
